{
  "name": "Customer Credit Note & Refund Processing Agent",
  "nodes": [
    {
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "id": "747b72ab-72ac-4b4b-9453-c4738b65c4f4",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        0
      ],
      "webhookId": "e69fd63a-4cf6-443e-ac3e-4d7bcb04f5d2",
      "credentials": {
        "telegramApi": {
          "id": "NFpPTqiHG1arwJ14",
          "name": "Telegram account"
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "id-1",
              "name": "customer_message",
              "value": "={{ $json.message.text }}",
              "type": "string"
            },
            {
              "id": "id-2",
              "name": "customer_name",
              "value": "={{ $json.message.from.first_name + ' ' + ($json.message.from.last_name || '') }}",
              "type": "string"
            },
            {
              "id": "id-3",
              "name": "chat_id",
              "value": "={{ $json.message.chat.id }}",
              "type": "string"
            }
          ]
        },
        "includeOtherFields": true,
        "options": {}
      },
      "id": "b2faa358-d82f-487c-996d-d5f0205d8afd",
      "name": "Workflow Configuration",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        224,
        0
      ]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=Customer Name: {{ $json.customer_name }}\nCustomer Message: {{ $json.customer_message }}\nChat ID: {{ $json.chat_id }}",
        "options": {
          "systemMessage": "=You are a Customer Credit Note and Refund Request Processing Assistant.\n\nYour responsibilities:\n1. Identify whether the customer wants to REGISTER a new credit note / refund request or CHECK the status of an existing request.\n2. For new requests, collect:\n   - Invoice Number\n   - Invoice Date\n   - Reason for Credit Note or Refund (Overbilling, Short Supply, Pricing Error, Return)\n   - Brief description\n3. Validate the invoice using the Invoice Master Sheet Tool and extract the Customer_Name.\n4. If the invoice is valid:\n   - Generate a unique credit request reference ID in format: CREDIT-YYYYMMDD-[last 4 digits of invoice]-HHmmss\n   - Register the request using the Credit Note Register Sheet Tool\n   - Send an acknowledgement email using the Gmail Tool\n   - Clearly explain approval workflow and expected timelines\n   - Respond to the customer using Telegram Response Tool\n5. If the invoice is invalid or not found:\n   - Politely explain the reason\n   - Do not register the request\n   - Respond using Telegram Response Tool\n6. For status checks:\n   - Ask for credit request reference ID if missing\n   - Retrieve current status using the Credit Status Sheet Tool\n   - Explain the current stage (Under Review / Approved / Rejected / Issued)\n   - Provide expected next update timeline\n   - Respond using Telegram Response Tool\n\nStrict rules:\n- Do NOT promise refunds or credit approval.\n- Do NOT discuss tax or accounting advice.\n- Be clear, calm, and professional.\n- Use simple business language.\n- ALWAYS use the Telegram Response Tool to send your final response."
        }
      },
      "id": "20d0be18-bcfc-450e-ba5e-ee00f5b44b5f",
      "name": "Insurance Claims Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3,
      "position": [
        496,
        0
      ]
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "id",
          "value": "gpt-4o"
        },
        "builtInTools": {},
        "options": {
          "temperature": 0.2
        }
      },
      "id": "7f788952-6b47-4dbc-91be-0b7e9cfe1a3a",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.3,
      "position": [
        320,
        224
      ],
      "credentials": {
        "openAiApi": {
          "id": "ICwxUBbatsF2sDvy",
          "name": "OpenAi account"
        }
      }
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $('Workflow Configuration').item.json.chat_id }}",
        "contextWindowLength": 10
      },
      "id": "82962153-520f-4a30-9df1-909b4f6505fe",
      "name": "Simple Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.3,
      "position": [
        480,
        224
      ]
    },
    {
      "parameters": {
        "descriptionType": "manual",
        "toolDescription": "Use this tool to validate invoice information. Provide invoice number to check if it exists and retrieve customer details.",
        "documentId": {
          "__rl": true,
          "value": "INVOICE_MASTER_SHEET_ID",
          "mode": "list"
        },
        "sheetName": {
          "__rl": true,
          "value": "Sheet1",
          "mode": "list"
        },
        "options": {}
      },
      "id": "9ccfaa85-e36f-420c-958f-c3a533d5eb75",
      "name": "Policy Master Sheet Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [
        640,
        224
      ]
    },
    {
      "parameters": {
        "descriptionType": "manual",
        "toolDescription": "Use this tool to register a new credit note or refund request.",
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "CREDIT_NOTE_REGISTER_SHEET_ID",
          "mode": "list"
        },
        "sheetName": {
          "__rl": true,
          "value": "Sheet1",
          "mode": "list"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Credit_Ref_ID": "={{ $fromAI('Credit_Ref_ID') }}",
            "Invoice_Number": "={{ $fromAI('Invoice_Number') }}",
            "Customer_Name": "={{ $fromAI('Customer_Name') }}",
            "Reason": "={{ $fromAI('Reason') }}",
            "Description": "={{ $fromAI('Description') }}",
            "Status": "={{ $fromAI('Status') }}",
            "Created_At": "={{ $fromAI('Created_At') }}"
          }
        },
        "options": {}
      },
      "id": "f1b8eced-5df9-4f24-8278-a3ab354a8205",
      "name": "Claims Register Sheet Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [
        784,
        224
      ]
    },
    {
      "parameters": {
        "descriptionType": "manual",
        "toolDescription": "Use this tool to retrieve the status of an existing credit note or refund request.",
        "documentId": {
          "__rl": true,
          "value": "CREDIT_STATUS_SHEET_ID",
          "mode": "list"
        },
        "sheetName": {
          "__rl": true,
          "value": "Sheet1",
          "mode": "list"
        },
        "options": {}
      },
      "id": "62745757-a7e6-4002-b850-fa6d36cf06be",
      "name": "Claims Status Sheet Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [
        928,
        224
      ]
    },
    {
      "parameters": {
        "descriptionType": "manual",
        "toolDescription": "Send credit note or refund acknowledgement email.",
        "sendTo": "={{ $fromAI('customer_email') }}",
        "subject": "={{ $fromAI('email_subject', 'Credit Note / Refund Request Received') }}",
        "message": "={{ $fromAI('email_body') }}",
        "options": {}
      },
      "id": "ba1015d8-26e0-481e-ac5f-e62fb4130470",
      "name": "Gmail Tool",
      "type": "n8n-nodes-base.gmailTool",
      "typeVersion": 2.1,
      "position": [
        1072,
        224
      ]
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "={{ $json.output }}",
        "additionalFields": {}
      },
      "id": "d74dc4e0-f783-4643-92e8-242ca666f514",
      "name": "Send a text message",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        928,
        0
      ]
    }
  ],
  "connections": {
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Workflow Configuration",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Workflow Configuration": {
      "main": [
        [
          {
            "node": "Insurance Claims Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Insurance Claims Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "Insurance Claims Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Policy Master Sheet Tool": {
      "ai_tool": [
        [
          {
            "node": "Insurance Claims Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Claims Register Sheet Tool": {
      "ai_tool": [
        [
          {
            "node": "Insurance Claims Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Claims Status Sheet Tool": {
      "ai_tool": [
        [
          {
            "node": "Insurance Claims Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Gmail Tool": {
      "ai_tool": [
        [
          {
            "node": "Insurance Claims Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Insurance Claims Agent": {
      "main": [
        [
          {
            "node": "Send a text message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  }
}
